/*
 * Copyright (c) 2017. 深圳掌控网络有限公司. All rights reserved.
 */

package com.yiqi.lottery.feature.find.presenter;

import android.text.TextUtils;
import android.widget.TextView;
import com.suozhang.framework.entity.bo.PageData;
import com.yiqi.lottery.entity.bo.BaseBo;
import com.yiqi.lottery.entity.bo.CircleBo;
import com.yiqi.lottery.entity.bo.FoucsBo;
import com.yiqi.lottery.feature.find.contract.CircleContract;
import com.yiqi.lottery.feature.find.dependencies.circle.DaggerCirclePresenterComponent;
import com.yiqi.lottery.feature.login.dependencies.login.DaggerLoginPresenterComponent;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

import javax.inject.Inject;

/**
 *      LIJUWEN
 * @email yiyayiyayaoljw@gmail.com
 * @date 2017/4/11 9:31
 */
public class CirclePresenter implements CircleContract.Presenter {

    private CircleContract.View mView;

    @Inject

    CircleContract.Model mModel;


    public CirclePresenter(CircleContract.View mView) {
        this.mView = mView;
        DaggerCirclePresenterComponent.create().inject(this);
    }

    @Override
    public void start() {

    }

    @Override
    public void doGetCircleList(int page, int post_type, int type) {
        mModel.getCircleList(page, post_type, type).compose(mView.bindToLife()).subscribe(new Observer<PageData<CircleBo>>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(PageData<CircleBo> s) {
                mView.dismissLoading();

                if (s.getList().size() > 0) {
                    mView.showCircleListSuccess(s);
                } else {
                    mView.showCircleListEmpty();
                }
            }

            @Override
            public void onError(Throwable e) {
                mView.dismissLoading();
                mView.showCircleListError(e.getMessage());
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }

    @Override
    public void doGetCircleMoreList(int page, int post_type, int type) {
        mModel.getCircleList(page, post_type, type).compose(mView.bindToLife()).subscribe(new Observer<PageData<CircleBo>>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(PageData<CircleBo> s) {
                mView.showCircleListMoreSuccess(s);

            }

            @Override
            public void onError(Throwable e) {

                mView.showCircleMoreErrorMsg(e.getMessage());
            }

            @Override
            public void onComplete() {

            }
        });
    }



    @Override
    public void getFoucedDetail(String id) {
        if (TextUtils.isEmpty(id)) {
            return;
        }
        mModel.getFoucedDetail(id).compose(mView.bindToLife()).subscribe(new Observer<FoucsBo>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(FoucsBo s) {
                mView.dismissLoading();
                mView.showDetailSuccess(s);
            }

            @Override
            public void onError(Throwable e) {
                mView.dismissLoading();
                mView.showCircleListError(e.getMessage());
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }

    @Override
    public void dianZan(Integer id) {
        if (id==null){
            return;
        }
        BaseBo baseBo = new BaseBo(id);
        mModel.dianZan(baseBo).compose(mView.bindToLife()).subscribe(new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(String s) {
                mView.showDaiaZanSuccess(s);
            }

            @Override
            public void onError(Throwable e) {

                mView.showDaiaZanError(e.getMessage());
            }

            @Override
            public void onComplete() {

            }
        });
    }

    @Override
    public void cancleDianzan(Integer id) {
        mModel.cancleDianzan(id).compose(mView.bindToLife()).subscribe(new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(String s) {

                mView.showDaiaZanSuccess(s);
            }

            @Override
            public void onError(Throwable e) {

                mView.showDaiaZanError(e.getMessage());
            }

            @Override
            public void onComplete() {

            }
        });
    }

    @Override
    public void getMyCircleList(int page) {
        mModel.getMyCircleList(page).compose(mView.bindToLife()).subscribe(new Observer<PageData<CircleBo>>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(PageData<CircleBo> s) {
                mView.dismissLoading();

                if (s.getList().size() > 0) {
                    mView.showCircleListSuccess(s);
                } else {
                    mView.showCircleListEmpty();
                }
            }

            @Override
            public void onError(Throwable e) {
                mView.dismissLoading();
                mView.showCircleListError(e.getMessage());
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }

    @Override
    public void getMyCircleMoreList(int page) {
        mModel.getMyCircleList(page).compose(mView.bindToLife()).subscribe(new Observer<PageData<CircleBo>>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(PageData<CircleBo> s) {
                mView.showCircleListMoreSuccess(s);
            }

            @Override
            public void onError(Throwable e) {
                mView.showCircleListError(e.getMessage());
            }

            @Override
            public void onComplete() {

            }
        });
    }

    @Override
    public void getJiaodian() {
        mModel.getJiaodian().compose(mView.bindToLife()).subscribe(new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(String s) {

                mView.showDaiaZanSuccess(s);
            }

            @Override
            public void onError(Throwable e) {

                mView.showDaiaZanError(e.getMessage());
            }

            @Override
            public void onComplete() {

            }
        });
    }

    @Override
    public void getZhongJiang() {
        mModel.getZhongJiang().compose(mView.bindToLife()).subscribe(new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(String s) {

                mView.showDaiaZanSuccess(s);
            }

            @Override
            public void onError(Throwable e) {

                mView.showDaiaZanError(e.getMessage());
            }

            @Override
            public void onComplete() {

            }
        });
    }
}
